#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   run.py
@Time    :   2020/09/30 15:05:27
@Author  :   fengzhilaoling 
@Version :   1.0
@Contact :   fengzhilaoling@163.com
'''
from flask import Flask
import logging
from logging.handlers import TimedRotatingFileHandler
import re


logger = logging.getLogger('mylogger')
# 设置日志级别
logger.setLevel(logging.DEBUG)
# 日志格式
formatter = '%(asctime)s: %(levelname)s %(filename)s-%(module)s-%(funcName)s-%(lineno)d %(message)s'
log_formatter = logging.Formatter(formatter)

# info日志处理器
# 通过设置 TimedRotatingFileHandler 进行日志按周(W)、天(D)、时(H)、分(M)、秒(S)切割。
info_handler = TimedRotatingFileHandler(filename='./log/info.log', when='M', interval=1, backupCount=7, encoding='utf-8')
info_handler.suffix = '%Y-%m-%d_%H-%M.log'
info_handler.setFormatter(log_formatter)
# 错误日志处理器
err_handler = TimedRotatingFileHandler(filename='./log/error.log', when='M', interval=1, backupCount=7, encoding='utf-8')
err_handler.suffix = '%Y-%m-%d_%H-%M.log'
err_handler.setFormatter(log_formatter)
# 添加日志处理器
logger.addHandler(info_handler)
logger.addHandler(err_handler)

app = Flask(__name__)
app.logger.addHandler(logger)



@app.route('/')
def root():
    app.logger.info('info log')
    app.logger.warning('warning log')
    return 'hello'

if __name__ == '__main__':
    app.run()
